/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ro.uvt.sp.lp.hw1.beermugs.visual;

/**
 * 
 * @author LP
 *
 */
public class BeerMugMove extends Move{

	public BeerMugMove(String moveName) {
		super(moveName);
        if(getMoveName().equals("Umple X")
                || getMoveName().equals("Umple Y") 
                || getMoveName().equals("Goleste X")
                || getMoveName().equals("Goleste Y")
                || getMoveName().equals("Transfera X in Y")
                || getMoveName().equals("Transfera Y in X")){
        }
        else{
            System.out.println("Operatie invalida");
        }
	}

	//executa o operatie
	 public State doMove(State otherState) {
			BeerMugState state = (BeerMugState) otherState;
		        if(!validMove(state)){
		            return null;            
		        }
		        int x, y;
		        x = state.getXLvl();
		        y = state.getYLvl();
		        switch(getMoveName()){
		            case("Umple X"):
		                x = 3;
		                break;
		            case("Umple Y"):
		                y = 4;
		                break;
		            case("Goleste X"):
		                x = 0;
		                break;
		            case("Goleste Y"):
		                y = 0;
		                break;
		            case("Transfera X in Y"):
		                if(y+x > 4){
		                    x = x - (4 - y);
		                    y=4;
		                    
		                }
		                else{
		                y = y+x;
		                x=0;
		                }

		                break;
		            case("Transfera Y in X"):

		                if(x+y > 3){
		                    y = y -(3-x);
		                    x = 3;
		                }
		                else{
		                x = x+y;
		                y=0;
		                }

		                break;
		        }
		        BeerMugState newState = new BeerMugState(x,y);
		        return newState;
		        
		    }
		    
		  
		    //validare operatie curenta
		    private boolean validMove(BeerMugState aState){
		        if(isXFull(aState)){
		            if(getMoveName().equals("Umple X") || getMoveName().equals("Transfera Y in X")){
		                return false;
		            }
		        }
		        else if(isXEmpty(aState)){
		            if(getMoveName().equals("Goleste X") || getMoveName().equals("Transfera X in Y")){
		                return false;
		            }
		        }
		        else if(isYFull(aState)){
		            if(getMoveName().equals("Umple Y") || getMoveName().equals("Transfera X in Y")){
		                return false;
		            }
		        }
		        
		        else if(isYEmpty(aState)){
		            if(getMoveName().equals("Goleste Y") || getMoveName().equals("Tranfera Y in X")){
		                return false;
		            }
		        }
		        return true;
		        
		    }
		        
		    //verifica dc X e plin
		    private boolean isXFull(BeerMugState state){
		        return(state.getXLvl() == 3);
		    }
		  //verifica dc Y e plin
		    private boolean isYFull(BeerMugState state){
		        return(state.getYLvl() == 4);
		    }
		    
		 //verifica dc Y e gol
		    private boolean isYEmpty(BeerMugState state){
		        return(state.getYLvl() == 0);
		    }
		    
		  //verifca dc X e gol
		    private boolean isXEmpty(BeerMugState state){
		        return(state.getXLvl() == 0);
		    }
	
	
}
